package com.jy.adscm.operation.mscyb.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.mscyb.bean.*;
import com.jy.adscm.operation.mscyb.mapper.mysql.*;
import com.jy.adscm.operation.mscyb.service.JqbzPartsassemblyService;
import com.jy.adscm.operation.mscyb.service.JqzzPartsMaterialListService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.operation.purchase.bean.Purchasebom;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-06-02
 */
@Service
public class JqzzPartsMaterialListServiceImpl extends ServiceImpl<JqzzPartsMaterialListMapper, JqzzPartsMaterialList> implements JqzzPartsMaterialListService {
    @Autowired
    JyUserMapper jyUserMapper;
    @Autowired
    JqbzPartsassemblyMapper jqbzPartsassemblyMapper;
    @Autowired
    JqbzSettingsMapper jqbzSettingsMapper;
    @Autowired
    JqbzPartsassemblyService jqbzPartsassemblyService;
    @Autowired
    JqbzPartsUseHistoryMapper jqbzPartsUseHistoryMapper;
    @Autowired
    JqzzPartsMaterialMapper jqzzPartsMaterialMapper;
    @Autowired
    JqzzMachineMapper jqzzMachineMapper;
    @Autowired
    JqzzPartsMaterialListCheckMapper jqzzPartsMaterialListCheckMapper;
    @Autowired
    JqbzSettingInCheckMapper jqbzSettingInCheckMapper;
    @Autowired
    JqbzMaterialMapper jqbzMaterialMapper;
    @Autowired
    JqbzMaterialListMapper jqbzMaterialListMapper;
    @Autowired
    JqbzAssemblyMapper jqbzAssemblyMapper;
    @Autowired
    JqbzSettingProecssMapper jqbzSettingProecssMapper;
    @Autowired
    JqbzMaterialListCheckMapper jqbzMaterialListCheckMapper;
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean insertJqzzPartsMaterialList(JqzzPartsMaterialList jqzzPartsMaterialList, Token token) throws AdscmSystemException {

            //根据这个查询部件库存中是否还存在余料
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqzzPartsMaterialList.getName(), null)) {

                //修改库存  减去库存  num相反数
                JqbzPartsassembly jqbzPartsassembly = jqbzPartsassemblyService.updateJqbzPartsassemblyValue(jqzzPartsMaterialList.getName(), -1);
                //完善信息
                if (AdscmBaseUtils.getInstance().isNotEmpty(jqbzPartsassembly.getTypenums(),null)){
                    jqzzPartsMaterialList.setType(jqbzPartsassembly.getTypenums());
                }
                if (AdscmBaseUtils.getInstance().isNotEmpty(jqbzPartsassembly.getBatchnums(),null)){
                    jqzzPartsMaterialList.setBatch(jqbzPartsassembly.getBatchnums());
                }
                if (AdscmBaseUtils.getInstance().isNotEmpty(jqbzPartsassembly.getTaskcode(),null)){
                    jqzzPartsMaterialList.setCode(jqbzPartsassembly.getTaskcode());
                }
                //添加用料信息
                jqzzPartsMaterialList.setCreateTime(new Date());
                jqzzPartsMaterialList.setCreator(token.getUser().getId().toString());
                jqzzPartsMaterialList.setNum(1);
                jqzzPartsMaterialList.setState(1);
                //默认使用时间为当前
                jqzzPartsMaterialList.setUseTime(new Date());
                super.insert(jqzzPartsMaterialList);

                //如果提交的数量和计划的数量相等，默认已经完成领料
                JqzzPartsMaterial jqzzPartsMaterial = jqzzPartsMaterialMapper.selectById(jqzzPartsMaterialList.getPid());
                Wrapper<JqzzPartsMaterialList> getAllWrapper=new EntityWrapper<>();
                getAllWrapper.eq("pid",jqzzPartsMaterial.getId());
                getAllWrapper.eq("state","1");
                List<JqzzPartsMaterialList> jqzzPartsMaterialLists = super.selectList(getAllWrapper);
                int tempNum=0;
                for (JqzzPartsMaterialList materialList : jqzzPartsMaterialLists) {
                    tempNum=tempNum+materialList.getNum();
                }
                if (jqzzPartsMaterial.getNum()<tempNum){
                    throw  new  AdscmSystemException("添加部件已超过设置用量"+jqbzPartsassembly.getTaskcode());
                }
                if (jqzzPartsMaterial.getNum()==tempNum){
                    jqzzPartsMaterial.setIsAssembly(1);
                    jqzzPartsMaterialMapper.updateById(jqzzPartsMaterial);
                }else {
                    jqzzPartsMaterial.setIsAssembly(0);
                    jqzzPartsMaterialMapper.updateById(jqzzPartsMaterial);
                }
                //出库信息
                //查出这个总装的信息
                JqzzMachine jqzzMachine = jqzzMachineMapper.selectById(jqzzPartsMaterial.getTid());
                JqbzPartsUseHistory jqbzPartsUseHistory =new JqbzPartsUseHistory();
                jqbzPartsUseHistory.setPid(jqbzPartsassembly.getTid());
                jqbzPartsUseHistory.setMid(jqzzPartsMaterialList.getId());
                jqbzPartsUseHistory.setCode(jqzzMachine.getTaskcode());
                jqbzPartsUseHistory.setState(1);
                jqbzPartsUseHistory.setCreator(token.getUser().getId().toString());
                jqbzPartsUseHistory.setCreateTime(new Date());
                jqbzPartsUseHistory.setRecorder(jqzzMachine.getTid().toString());
                jqbzPartsUseHistory.setRecorderName(jqzzMachine.getResourcename());
                jqbzPartsUseHistoryMapper.insert(jqbzPartsUseHistory);
                //生成检验记录

                Wrapper<JqbzSettingInCheck> wrapper =new EntityWrapper<>();
                wrapper.eq("pid",jqzzPartsMaterial.getName());
                wrapper.eq("isEnable",1);
                List<JqbzSettingInCheck> JqbzSettingInChecks = jqbzSettingInCheckMapper.selectList(wrapper);
                if (JqbzSettingInChecks!=null && JqbzSettingInChecks.size()>0 ){
                    for (JqbzSettingInCheck jqbzSettingInCheck : JqbzSettingInChecks) {
                        JqzzPartsMaterialListCheck  jqzzPartsMaterialListCheck =new JqzzPartsMaterialListCheck();
                        jqzzPartsMaterialListCheck.setPid(jqzzPartsMaterialList.getId());
                        jqzzPartsMaterialListCheck.setCid(jqbzSettingInCheck.getId());
                        jqzzPartsMaterialListCheck.setName(jqbzSettingInCheck.getName());
                        jqzzPartsMaterialListCheck.setReference(jqbzSettingInCheck.getReference());
                        jqzzPartsMaterialListCheck.setType(jqbzSettingInCheck.getType());
                        jqzzPartsMaterialListCheck.setUnit(jqbzSettingInCheck.getUnit());
                        jqzzPartsMaterialListCheck.setRemark(jqbzSettingInCheck.getRemark());
                        jqzzPartsMaterialListCheck.setRank(jqbzSettingInCheck.getRank());
                        jqzzPartsMaterialListCheckMapper.insert(jqzzPartsMaterialListCheck);
                    }
                    jqzzPartsMaterialList.setIsCheck(1);
                }else {
                    jqzzPartsMaterialList.setState(1);
                    jqzzPartsMaterialList.setIsCheck(0);
                }
                super.updateById(jqzzPartsMaterialList);
            }

        return true;
    }
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean deleteJqzzPartsMaterialList(Integer id) throws AdscmSystemException {

        //1.查询出该条记录
        JqzzPartsMaterialList jqzzPartsMaterialList = super.selectById(id);
        //2.修改回原来的库存状态
        jqbzPartsassemblyService.updateJqbzPartsassemblyValue(jqzzPartsMaterialList.getName(), 1);
        ///3.删除掉对应的库存出库记录
        Wrapper<JqbzPartsUseHistory> wrapper =new EntityWrapper<>();
        wrapper.eq("mid",id);
        List<JqbzPartsUseHistory> jqbzPartsUseHistories = jqbzPartsUseHistoryMapper.selectList(wrapper);
        if (jqbzPartsUseHistories!=null&& jqbzPartsUseHistories.size()>0){
            JqbzPartsUseHistory jqbzPartsUseHistory = jqbzPartsUseHistories.get(0);
            jqbzPartsUseHistoryMapper.deleteById(jqbzPartsUseHistory.getId());
        }
        //删除对应的检验记录
        Wrapper<JqzzPartsMaterialListCheck> jqzzMaterialListCheckwrapper=new EntityWrapper<>();
        jqzzMaterialListCheckwrapper.eq("pid",id);
        List<JqzzPartsMaterialListCheck> jqzzPartsMaterialListChecks = jqzzPartsMaterialListCheckMapper.selectList(jqzzMaterialListCheckwrapper);
        if (jqzzPartsMaterialListChecks!=null && jqzzPartsMaterialListChecks.size()>0){
            JqzzPartsMaterialListCheck jqzzPartsMaterialListCheck = jqzzPartsMaterialListChecks.get(0);
            jqzzPartsMaterialListCheckMapper.deleteById(jqzzPartsMaterialListCheck);
        }
        boolean b = super.deleteById(id);
        return b;
    }
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean updateJqzzPartsMaterrialList(JqzzPartsMaterialList jqzzPartsMaterialList) throws AdscmSystemException {
        JqbzPartsassembly jqbzPartsassembly = jqbzPartsassemblyMapper.selectById(jqzzPartsMaterialList.getName());
        jqbzPartsassembly.setState(jqzzPartsMaterialList.getState());
        if (!"1".equals(jqzzPartsMaterialList.getState())){
            jqbzPartsassembly.setIsoutstorage(0);
        }
        Wrapper<JqbzPartsUseHistory> wrapper =new EntityWrapper<>();
        wrapper.eq("mid",jqzzPartsMaterialList.getId());
        List<JqbzPartsUseHistory> jqbzPartsUseHistories = jqbzPartsUseHistoryMapper.selectList(wrapper);
        if (jqbzPartsUseHistories!=null && jqbzPartsUseHistories.size()>0 ){
            JqbzPartsUseHistory jqbzPartsUseHistory = jqbzPartsUseHistories.get(0);
            jqbzPartsUseHistory.setState(jqzzPartsMaterialList.getState());
            jqbzPartsUseHistoryMapper.updateById(jqbzPartsUseHistory);
        }
        //判断是否判断为不合格的
        if (jqzzPartsMaterialList.getState()==0){
            jqbzPartsassembly.setIsreviewtask("1");
            //新建一条不合格任务
            JqbzPartsassembly  newjqbzPartsassembly =jqbzPartsassembly;
            newjqbzPartsassembly.setTid(null);
            newjqbzPartsassembly.setIstaskstart("0");
            newjqbzPartsassembly.setIstaskbegin("0");
            newjqbzPartsassembly.setIstaskend("0");
            newjqbzPartsassembly.setIsreviewtask("0");
            newjqbzPartsassembly.setReviewresult("-1");
            newjqbzPartsassembly.setTaskbeginaddtime(null);
            newjqbzPartsassembly.setTaskstartaddtime(null);
            newjqbzPartsassembly.setTaskperformperson(null);
            newjqbzPartsassembly.setTaskstartaddtime(null);
            newjqbzPartsassembly.setTaskbegintime(null);
            newjqbzPartsassembly.setTaskendtime(null);
            if (AdscmBaseUtils.getInstance().isNotEmpty( newjqbzPartsassembly.getRepair(),null)){
                newjqbzPartsassembly.setRepair(String.valueOf(Integer.valueOf(newjqbzPartsassembly.getRepair())+1));
            }else {
                newjqbzPartsassembly.setRepair("1");
            }
            jqbzPartsassemblyMapper.insert(newjqbzPartsassembly);
            //生成对应的物料清单
            Wrapper<JqbzMaterial> jqbzMaterialWrapper =new EntityWrapper<>();
            jqbzMaterialWrapper.eq("tid", jqbzPartsassembly.getTid());
            List<JqbzMaterial> jqbzMaterials = jqbzMaterialMapper.selectList(jqbzMaterialWrapper);
            for (JqbzMaterial jqbzMaterial : jqbzMaterials) {
              //依次生成列表
                JqbzMaterial newjqbzMaterial=jqbzMaterial;
                newjqbzMaterial.setId(null);
                newjqbzMaterial.setTid(newjqbzPartsassembly.getTid());
                jqbzMaterialMapper.insert(newjqbzMaterial);
                //查询详情生成
                Wrapper<JqbzMaterialList> jqbzMaterialListWrapper =new EntityWrapper<>();
                jqbzMaterialListWrapper.eq("pid",jqbzMaterial.getId());
                List<JqbzMaterialList> jqbzMaterialLists = jqbzMaterialListMapper.selectList(jqbzMaterialListWrapper);
                for (JqbzMaterialList jqbzMaterialList : jqbzMaterialLists) {
                    JqbzMaterialList newjqbzMaterialList= jqbzMaterialList;
                    newjqbzMaterialList.setId(null);
                    newjqbzMaterialList.setPid(newjqbzMaterial.getId());
                    jqbzMaterialListMapper.insert(newjqbzMaterialList);
                    //查询这个检测记录
                    Wrapper<JqbzMaterialListCheck> jqbzMaterialListCheckWrapper=new EntityWrapper<>();
                    jqbzMaterialListCheckWrapper.eq("pid",jqbzMaterialList.getId());
                    List<JqbzMaterialListCheck> jqbzMaterialListChecks = jqbzMaterialListCheckMapper.selectList(jqbzMaterialListCheckWrapper);
                    for (JqbzMaterialListCheck jqbzMaterialListCheck : jqbzMaterialListChecks) {
                        JqbzMaterialListCheck newjqbzMaterialListCheck=jqbzMaterialListCheck;
                        newjqbzMaterialListCheck.setId(null);
                        newjqbzMaterialListCheck.setPid(newjqbzMaterialList.getId());
                        jqbzMaterialListCheckMapper.insert(newjqbzMaterialListCheck);
                    }
                }

            }
            //生成新的过程检测记录
            Wrapper<JqbzSettingProecss> jqbzSettingProecssWrapper=new EntityWrapper<>();
            jqbzSettingProecssWrapper.eq("pid",jqbzPartsassembly.getResourcename());
            jqbzSettingProecssWrapper.eq("isEnable","1");
            List<JqbzSettingProecss> jqbzSettingProecsses = jqbzSettingProecssMapper.selectList(jqbzSettingProecssWrapper);
            for (JqbzSettingProecss jqbzSettingProecss : jqbzSettingProecsses) {
                JqbzAssembly jqbzAssembly =new JqbzAssembly();
                jqbzAssembly.setPid(newjqbzPartsassembly.getTid());
                jqbzAssembly.setCid(jqbzSettingProecss.getId());
                jqbzAssembly.setName(jqbzSettingProecss.getName());
                jqbzAssembly.setReference(jqbzSettingProecss.getReference());
                jqbzAssembly.setUnit(jqbzSettingProecss.getUnit());
                jqbzAssembly.setType(jqbzSettingProecss.getType());
                jqbzAssembly.setRank(jqbzSettingProecss.getRank());
                jqbzAssembly.setRemark(jqbzSettingProecss.getRemark());
                jqbzAssemblyMapper.insert(jqbzAssembly);
            }
            jqbzPartsassembly.setState(0);
        }


        jqbzPartsassemblyMapper.updateById(jqbzPartsassembly);

        boolean b = super.updateById(jqzzPartsMaterialList);
        return b;
    }

    @Override
    public List<JqzzPartsMaterialList> selectByTid(String tid) throws AdscmSystemException {
        Wrapper<JqzzPartsMaterialList> wrapper =new EntityWrapper<>();
        wrapper.eq("pid",tid);
        List<JqzzPartsMaterialList> jqzzPartsMaterialLists = super.selectList(wrapper);
        for (JqzzPartsMaterialList jqzzPartsMaterialList : jqzzPartsMaterialLists) {
            JqbzPartsassembly jqbzPartsassembly = jqbzPartsassemblyMapper.selectById(jqzzPartsMaterialList.getName());
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqbzPartsassembly,null)){
                JqbzSettings jqbzSettings = jqbzSettingsMapper.selectById(jqbzPartsassembly.getResourcename());
                jqzzPartsMaterialList.setNameName(jqbzSettings.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqzzPartsMaterialList.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(jqzzPartsMaterialList.getCreator());
                if(AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                    jqzzPartsMaterialList.setCreatorName(jyUser.getName());
                }
            }
        }

        return jqzzPartsMaterialLists;
    }

    @Override
    public List<JqbzPartsassembly> queryMaterial(String id,String code) throws AdscmSystemException {
        Wrapper<JqbzPartsassembly> wrapper=new EntityWrapper<>();
        wrapper.setSqlSelect("TID","RESOURCENAME","TASKCODE");
        if (AdscmBaseUtils.getInstance().isNotEmpty(code,null)){
            wrapper.eq("TASKCODE",code);
        }
        wrapper.eq("RESOURCENAME",id);
        wrapper.eq("ISTASKBEGIN",1);
        wrapper.eq("ISTASKEND",1);
        wrapper.eq("ISREVIEWTASK",0);
        wrapper.eq("ISOUTSTORAGE",0);
        wrapper.eq("state",1);
            List<JqbzPartsassembly> jqbzPartsassemblys = jqbzPartsassemblyMapper.selectList(wrapper);
            if(!(jqbzPartsassemblys!=null&&jqbzPartsassemblys.size()>0)){
                JqbzSettings jqbzSettings = jqbzSettingsMapper.selectById(id);
                throw new AdscmSystemException("部件库中无库存"+jqbzSettings.getName());
            }
            for (JqbzPartsassembly jqbzPartsassembly : jqbzPartsassemblys) {
                if (AdscmBaseUtils.getInstance().isNotEmpty(jqbzPartsassembly.getResourcename(),null)){
                    JqbzSettings jqbzSettings = jqbzSettingsMapper.selectById(jqbzPartsassembly.getResourcename());
                    jqbzPartsassembly.setResourcenameName(jqbzSettings.getName());
                }
            }
            return jqbzPartsassemblys;
    }
}
